package com.DHH.Controller;

import com.DHH.Config.Constant;
import com.DHH.Entity.Base.BaseMenu;
import com.DHH.Entity.Base.BaseRole;
import com.DHH.Entity.Base.BaseUser;
import com.DHH.Mapper.Base.BaseMenuMapper;
import com.DHH.Mapper.Base.BaseUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/Admin")
public class HomeController {
    @Autowired
    private BaseMenuMapper baseMenuMapper;
    @Autowired
    private BaseUserMapper baseUserMapper;
    @RequestMapping("/home")
    public String home(ModelMap modelMap)
    {
        Set<BaseMenu> menuSet=new HashSet<BaseMenu>();
        Session session= SecurityUtils.getSubject().getSession();
        BaseUser user= (BaseUser) session.getAttribute(Constant.USER_IDENTIFICATION);
        if(user==null)
        {
            return "admin/login.jsp";
        }
        if(user.getBaseRoleSet()==null) {
            user.setBaseRoleSet(new HashSet<BaseRole>());
        }
        for(BaseRole role:user.getBaseRoleSet()) {

            List<BaseMenu> list= baseMenuMapper.selectMenuByRoleId(role.getId());
            menuSet.addAll(list);
        }
        user=baseUserMapper.selectByPrimaryKey(user);
        modelMap.put("menuList",menuSet);
        modelMap.put("sysUser",user);
        return "admin/index.jsp";
    }

}
